<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Commodity extends CI_Controller {
	
	var $_data	= array();
	
	function __construct()
	{
		parent::__construct();
//		if(!$this->session->userdata('is_admin_login')){
//			$this->load->helper('url');
//			redirect('/admin/auth/');
//		}

		$this->seg_exp = segment_explode($this->uri->uri_string());
		$this->load->model('adm/commodity_m');
		
		$this->load->config('zs_config');
		$this->load->helper('html');
		
			
		$this->load->model('/admin/category_mo', 'category_mo');
		$this->_data['category_tree'] = $this->category_mo->get_all_category('0', 1);
		
	}
	
	public function _remap($method, $params = array())
	{
		if( method_exists($this, $method) )
		{
			return call_user_func_array(array($this, $method), $params);
		} else if ($method == 'page') {
			$this->search();
		}
	}
	
	public function index(){
		//$this->search();
		$this->load->library('form_validation');
 		$this->load->view('admin/common/header');
 		
 		//検索条件初期値
 		$param = array();
 		$param["s_stock"] = '1';
 		$param["s_sale_flg"] = array('0','1');
 		$this->_data['param'] = $param;
 		
 		
 		$this->load->view('admin/commodity/commodity_list', $this->_data);
 		$this->load->view('admin/common/footer');
	}
	
	public function search(){

		$this->url_seg = $this->seg_exp;
		
		$param = array();
		
		$param["s_category"] = $this->input->post('s_category');
		$param["s_commoditycode_from"] = $this->input->post('s_commoditycode_from');
		$param["s_commoditycode_to"] = $this->input->post('s_commoditycode_to');
		$param["s_skucode"] = $this->input->post('s_skucode');
		$param["s_commodity_name"] = $this->input->post('s_commodity_name');
		$param["s_keyword"] = $this->input->post('s_keyword');
		$param["s_sales_start_from"] = $this->input->post('s_sales_start_from');
		$param["s_sales_start_to"] = $this->input->post('s_sales_start_to');
		$param["s_sales_end_from"] = $this->input->post('s_sales_end_from');
		$param["s_sales_end_to"] = $this->input->post('s_sales_end_to');
		$param["s_stock"] = $this->input->post('s_stock');
		
		$checkbox = $this->input->post('s_sale_flg');
		$param["s_sale_flg"] = $checkbox;
		
		
		
		$this->_data['param'] = $param;
		
		
		$this->load->library('form_validation');
// 		$this->form_validation->set_rules('s_commoditycode_from', '제목', 'required');
// 		$this->form_validation->run();
	
		$this->load->view('admin/common/header');
		//$list = $this->commodity_model->searchLists($param);
		
		
		//あと,共通化
		if($this->uri->segment(4) == 'page')
		{
			$this->_data['page_account']= $page = 1;
		}
		else
		{
			//페이징
			if( in_array('page', $this->seg_exp) )
			{
				$this->_data['page_account'] = $page = urldecode($this->security->xss_clean(url_explode($this->seg_exp, 'page')));
			}
			else
			{
				$this->_data['page_account'] = $page = 1;
			}
		}
		
		$rp    = 20; //리스트 갯수(表示数)
		$limit = 10; //보여줄 페이지수
		
		$start = (($page-1) * $rp);
		//$start = 1;
		
		
		//$list = $this->commodity_model->searchLists($param,$start, $rp);
		$list = $this->commodity_m->get_commodity_lists($param,$start, $rp);
				
		//$data['rs']           = $this->commodity_model->gets($start, $rp);
		$this->_data['rs']           = $list;
		
		//$data['search_total'] = $total = $this->commodity_model->gets_total(); //$this->commodity_model->gets_total();
		//$data['search_total'] = $total = 1400; //$this->commodity_model->gets_total();
		$this->_data['search_total'] = $total = $this->commodity_m->gets_total($param);
		if( in_array('page', $this->seg_exp) )
		{
		
			$urls = implode('/', url_delete($this->url_seg, 'page'));
		}
		else
		{
			$urls = implode('/', $this->url_seg);
		}
		
		//TEST確認用
		//echo $urls.':'.$page.':'.$rp.':'.$total.':'.$limit.':'.$page;
		
		//$data['pagination_links'] = pagination_common($urls."/page", paging($page,$rp,$total,$limit));
		
		
		$this->_data['list'] = $list;
		$this->_data['condition'] = $param;
		
		//$this->load->view('admin/commodity/commodity_list', array('list'=>$list,'condition'=>$param));
		$this->load->view('admin/commodity/commodity_list', $this->_data);
		$this->load->view('admin/common/footer');
	}
	
	public function commodity_edit($param,$sku_code){
		$this->load->view('admin/common/header');
		$detail = $this->commodity_m->search_detail($sku_code);
		$this->load->view('admin/commodity/detail', array('detail'=>$detail));		
		$this->load->view('admin/common/footer');
	}

	public function upload_receive(){
	
		// 사용자가 업로드 한 파일을 /static/user/ 디렉토리에 저장한다.
		$config['upload_path'] = './static/user/';
		// git,jpg,png 파일만 업로드를 허용한다.
		$config['allowed_types'] = 'gif|jpg|png';
		// 허용되는 파일의 최대 사이즈
		$config['max_size'] = '100';
		// 이미지인 경우 허용되는 최대 폭
		$config['max_width']  = '1024';
		// 이미지인 경우 허용되는 최대 높이
		$config['max_height']  = '768';
		$this->load->library('upload', $config);
	
		if ( ! $this->upload->do_upload("user_upload_file"))
		{
			echo $this->upload->display_errors();
		}
		else
		{
			$data = array('upload_data' => $this->upload->data());
	
			//$this->load->view('upload_success', $data);
			echo 'success';
			var_dump($data);
		}
	
	}
}

